#include<iostream>
#include<string>
using namespace std;
typedef long long LL;
const int N=5e5+10;

int main(){
	string op;
	int n;
	cin>>n>>op;
	LL ans=0;
	for(int i=0;i<n;i++){
		int l=0,r=0;
		int a=i-1,b=i+1;
		if(op[i]=='H'){
			while(a>=0&&op[a]!='H'){
				a--;
				l++;
			} 
			while(b<n&&op[b]!='H'){
				b++;
				r++;
			}
			if(l+r<2)continue;
			else{
				if(r>=2)ans+=r-1;
				if(l>=2)ans+=l-1;
				ans+=l*r;
			} 
		}else{
				while(a>=0&&op[a]!='G'){
				a--;
				l++;
			} 
			while(b<n&&op[b]!='G'){
				b++;
				r++;
			}
			if(l+r<2)continue;
			else{
				if(r>=2)ans+=r-1;
				if(l>=2)ans+=l-1;
				ans+=(LL)l*r;
			} 
		} 
	}
	cout<<ans;	
	return 0;
}
